package br.com.fiap.goaround.bo;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import br.com.fiap.goaround.dao.TrafficDAO;
import br.com.fiap.goaround.parser.GenerateTrafficBase;
import br.com.fiap.goaround.to.TrafficTO;

public class TrafficBO {
	
	private TrafficDAO dao = null;
	
	public TrafficBO(){
		
		this.dao = new TrafficDAO();

	}
	
	public boolean updateBase() throws Exception {

		Long tempo = System.currentTimeMillis();

		DateFormat dtf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Date datainicial = new Date();

		System.out.println("Inicio Geral: " + dtf.format(datainicial));
		
		// Expurga a base
		deleteAll();
		
		//Insere na base
		insertBase();
		
		Date datafinal = new Date();
		System.out.println("Final Geral: " + dtf.format(datafinal));

		System.out.println("Tempo Total Geral: "
				+ (System.currentTimeMillis() - tempo));
		
		return true;
		
	}
	
	public List<TrafficTO> findAll(){
		TrafficDAO dao = new TrafficDAO();
		return  dao.findAll();
	}
	
	public void insertBase() throws IOException{

		DateFormat dtf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Long tempoins = System.currentTimeMillis();
		Date datains = new Date();
		
		System.out.println("Inicio inclusao: " + dtf.format(datains));
		
		//Collection<TabStatusEndereco> endbom = new ArrayList<TabStatusEndereco>();
		Collection<TrafficTO> endruim = new ArrayList<TrafficTO>();
		GenerateTrafficBase gbt = new GenerateTrafficBase();
		//endruim = gbt.getBadList();
		endruim = gbt.getGoodList();

		TrafficDAO dao = new TrafficDAO();
		dao.insertAll(endruim);

		datains = new Date();
		
		System.out.println("Fim da inclusao: " + dtf.format(datains));
		System.out.println("Tempo Total do Expurgo: "
				+ (System.currentTimeMillis() - tempoins));
		
	}

	public void deleteAll(){

		dao.deleteAll();
		
	}
	
	
}
